#include "memWordWidget.h"

memWordWidget::memWordWidget():ZKbMainWidget(ZHeader::MAINDISPLAY_HEADER, NULL, "memWorldWidget", 0)
{
	qDebug("app start --------------");
	setMainWidgetTitle(cntr("查单词"));
	createTabWidget();
	SoftKey = new ZSoftKey(NULL, this, this);
	setSoftKeySearch();
	setSoftKey(SoftKey);
}
memWordWidget::~memWordWidget()
{
    printf("memWordWidget destructor\n");
	delete SoftKey;
	delete mMemWordTab;
	delete mMemWordTab2;
	delete msearchWordWidget;
	delete tabWidget;
}

void memWordWidget::setSoftKeySearch()
{
	//QRect rect;
	SoftKey->setText(ZSoftKey::LEFT, cntr("添加"));
	SoftKey->setText(ZSoftKey::RIGHT, cntr("清除"));
	SoftKey->setClickedSlot(ZSoftKey::RIGHT, msearchWordWidget, SLOT(clearEngStr()));
	SoftKey->setClickedSlot(ZSoftKey::LEFT, msearchWordWidget, SLOT(addCurrentWord()));
}

void memWordWidget::setSoftKeyMem()
{
	SoftKey->setText(ZSoftKey::LEFT, cntr("记单词"));
	SoftKey->setText(ZSoftKey::RIGHT, cntr("真好玩"));
	int pageIndex = tabWidget->currentPageIndex();
	if(pageIndex == 1)
	{
		SoftKey->setClickedSlot(ZSoftKey::LEFT, mMemWordTab, SLOT(yesFunc()));
		SoftKey->setClickedSlot(ZSoftKey::RIGHT, mMemWordTab, SLOT(noFunc()));

		connect(mMemWordTab, SIGNAL(updateSoftKey()), this, SLOT(slotMemWordSoftKeyChanged()));
		connect(mMemWordTab, SIGNAL(updateSoftKey()), this, SLOT(slotMemWordSoftKeyChanged()));
	}else if(pageIndex == 2)
	{
		SoftKey->setClickedSlot(ZSoftKey::LEFT, mMemWordTab2, SLOT(yesFunc()));
		SoftKey->setClickedSlot(ZSoftKey::RIGHT, mMemWordTab2, SLOT(noFunc()));

		connect(mMemWordTab2, SIGNAL(updateSoftKey()), this, SLOT(slotMemWordSoftKeyChanged()));
		connect(mMemWordTab2, SIGNAL(updateSoftKey()), this, SLOT(slotMemWordSoftKeyChanged()));
	}
}

void memWordWidget::createTabWidget()
{
	RES_ICON_Reader iconReader;

	tabWidget = new ZNavTabWidget(this, "tabwidget");

	msearchWordWidget = new searchWordWidget(this);
	tabWidget->addTab(msearchWordWidget, iconReader.getIcon("fm_memory_card_tab.bmp"), cntr("记单词"));


	
	mMemWordTab = new memWordTab(this,QString("/mmc/mmca1/qttest/dict/cet6.db"));
	tabWidget->addTab(mMemWordTab, iconReader.getIcon("fm_memory_card_tab.bmp"), cntr("背单词"));
	
	mMemWordTab2 = new memWordTab(this,QString("/mmc/mmca1/qttest/dict/unKnowWords.db"));
	tabWidget->addTab(mMemWordTab2, iconReader.getIcon("fm_memory_card_tab.bmp"), cntr("未记住单词"));

	QVBoxLayout *vbl = getVBoxLayout();
	vbl->addWidget(tabWidget);
    connect(tabWidget, SIGNAL(currentChanged(QWidget*)), this, SLOT(slotCurrentChanged(QWidget*)));
    //connect(tabWidget, SIGNAL(currentChanged(QWidget*)), this, SLOT(slotPageChanged(QWidget*)));
}

void memWordWidget::slotMemWordSoftKeyChanged()
{
	int pageIndex = tabWidget->currentPageIndex();
	int statue;
	if(pageIndex == 1) statue=mMemWordTab->statue;
	else if(pageIndex == 2) statue=mMemWordTab2->statue;
	switch(statue)
	{
		case 1:
			SoftKey->setText(ZSoftKey::LEFT, QString(cntr("认识")));
			SoftKey->setText(ZSoftKey::RIGHT, QString(cntr("不认识")));
			break;
		case 2:
			SoftKey->setText(ZSoftKey::LEFT, QString(cntr("正确")));
			SoftKey->setText(ZSoftKey::RIGHT, QString(cntr("错误")));
			break;
		case 3:
			SoftKey->setText(ZSoftKey::LEFT, QString(cntr("下一个")));
			SoftKey->setText(ZSoftKey::RIGHT, QString(cntr("下一个")));
			break;
		default:
				printf("unKnow statue\n");
	}
}

void memWordWidget::slotCurrentChanged(QWidget *)
{
	int pageIndex = tabWidget->currentPageIndex();
	printf("pageIndex = %d\n", pageIndex);
	if(pageIndex == 0)
	{
		setMainWidgetTitle(cntr("查单词"));
		printf("setSoftKey of searchSoftKey\n");
		setSoftKeySearch();
		printf("setSoftKey of searchSoftKey over\n");
	}
	else if(pageIndex == 1)
	{
		printf("set MemWord SoftKey\n");
		setMainWidgetTitle(cntr("记单词(cet6)"));
		setSoftKeyMem();
		slotMemWordSoftKeyChanged();
	}
	else if(pageIndex == 2)
	{
		printf("set MemWord SoftKey\n");
		setMainWidgetTitle(cntr("记单词(生词)"));
		setSoftKeyMem();
		slotMemWordSoftKeyChanged();
	}
}

void memWordWidget::keyPressEvent(QKeyEvent *e)
{
	int pageIndex = tabWidget->currentPageIndex();
	// 由于不能focus(),所以手动分发按键事件
	if(pageIndex == 0)
		msearchWordWidget->keyPressEvent(e);
	else if(pageIndex == 1)
		mMemWordTab->PressEvent(e);
	else if(pageIndex == 2)
		mMemWordTab2->PressEvent(e);
}

void memWordWidget::keyReleaseEvent(QKeyEvent *e)
{
	int pageIndex = tabWidget->currentPageIndex();
	if(e->key() == KEY_HANGDOWN)
		qApp->quit();
	// 由于不能focus(),所以手动分发按键事件
	if(pageIndex == 1)
		mMemWordTab->ReleaseEvent(e);
	else if(pageIndex == 2)
		mMemWordTab2->ReleaseEvent(e);
}

